package com.example.demo.tool;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.demo.entity.TbUser;
import com.example.demo.mapper.TbUserMapper;
import com.example.demo.record.UserRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName TbUserTools
 * @Tool:
 * @Author: hgq
 * @CreateDate: 2025/4/23 11:29
 * @UpdateUser: hgq
 * @UpdateDate: 2025/4/23 11:29
 * @UpdateRemark:
 * @Version: 1.0
 */
@Slf4j
@Component
public class TbUserTools {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Tool(description = "新增用户")
    public String saveUserFunction(UserRecord userRecord) {
        log.info("新增用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start");
        if (StrUtil.isEmpty(userRecord.id())) {
            return "没有主键id不能保存";
        }
        String nowDate = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
        TbUser user = TbUser.
                builder()
                .id(userRecord.id())
                .userName(userRecord.userName())
                .email(userRecord.email())
                .password(userRecord.password())
                .createTime(nowDate)
                .updateTime(nowDate)
                .build();
        int insert = tbUserMapper.insert(user);
        return "保存成功";
    }

    @Tool(description = "修改用户")
    public String updateUserFunction(UserRecord userRecord) {
        log.info("修改用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start");
        TbUser tbUser = tbUserMapper.selectById(userRecord.id());
        if (tbUser == null) {
            return "没有查询到需要修改的用户";
        }
        String nowDate = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
        tbUserMapper.update(null, new LambdaUpdateWrapper<TbUser>()
                .set(StrUtil.isNotEmpty(userRecord.userName()), TbUser::getUserName, userRecord.userName())
                .set(StrUtil.isNotEmpty(userRecord.email()), TbUser::getEmail, userRecord.email())
                .set(StrUtil.isNotEmpty(userRecord.password()), TbUser::getPassword, userRecord.password())
                .set(TbUser::getUpdateTime, nowDate)
                .eq(TbUser::getId, userRecord.id())
        );
        return "成功";
    }

    @Tool(description = "查询所有用户")
    public List<TbUser> queryAllUserFunction(UserRecord userRecord) {
        log.info("查询所有用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start");
        List<TbUser> list = tbUserMapper.selectList(new QueryWrapper<>());

        log.info("查询所有用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>end");
        return list;
    }

    @Tool(description = "根据条件查询用户")
    public List<TbUser> queryByUserFunction(UserRecord userRecord) {
        log.info("根据条件查询用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start");
        List<TbUser> list = tbUserMapper.selectList(null, new LambdaQueryWrapper<TbUser>()
                .like(StrUtil.isNotEmpty(userRecord.userName()), TbUser::getUserName, userRecord.userName())
                .like(StrUtil.isNotEmpty(userRecord.email()), TbUser::getEmail, userRecord.email())
                .like(StrUtil.isNotEmpty(userRecord.password()), TbUser::getPassword, userRecord.password())
                .eq(StrUtil.isNotEmpty(userRecord.id()), TbUser::getId, userRecord.id())
        );

        log.info("根据条件查询用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>end");
        return list;
    }

    @Tool(description = "根据id删除用户")
    public String removeUserByIdFunction(UserRecord userRecord) {
        log.info("根据id删除用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>start");
        if (StrUtil.isEmpty(userRecord.id())) {
            return "需要提供id才能删除";
        }

        TbUser tbUser = tbUserMapper.selectById(userRecord.id());
        if (tbUser == null) {
            return "没有查询到需要删除用户";
        }

        tbUserMapper.deleteById(userRecord.id());
        return "成功";
    }
}
